/*
 * timeout.cpp
 *
 *  Created on: Jan 16, 2014
 *      Author: yosef
 */

#include <stdio.h>
#include "timeout.h"


// Reset timer to current time
void TimeOut::ResetTimer(void)
{
    gettimeofday(&PreviousTime, NULL);
}

// Return the elapsed time since reset timer
unsigned long int TimeOut::ElapsedTime_ms(void)
{
    struct timeval CurrentTime;
    long int sec, usec;

    gettimeofday(&CurrentTime, NULL);
    sec = CurrentTime.tv_sec - PreviousTime.tv_sec;
    if(sec < 0) {
    	sec = 60 - PreviousTime.tv_sec + CurrentTime.tv_sec;
    }

    usec = CurrentTime.tv_usec - PreviousTime.tv_usec;
    if (usec < 0) {
        usec = 1000000 - PreviousTime.tv_usec + CurrentTime.tv_usec;
        sec--;
    }
    return sec * 1000 + usec / 1000;
}


// Return the elapsed time since reset timer
unsigned long int TimeOut::ElapsedTime_us(void)
{
    struct timeval CurrentTime;
    long int sec, usec;

    gettimeofday(&CurrentTime, NULL);
    sec = CurrentTime.tv_sec - PreviousTime.tv_sec;
    if(sec < 0) {
    	sec = 60 - PreviousTime.tv_sec + CurrentTime.tv_sec;
    }

    usec = CurrentTime.tv_usec - PreviousTime.tv_usec;
    if (usec < 0) {
        usec = 1000000 - PreviousTime.tv_usec + CurrentTime.tv_usec;
        sec--;
    }
    return sec * 1000000 + usec;
}



